package com.symbio.ishenapi.background;


import com.symbio.ishenapi.background.controller.TmHospitalInfoController;
import com.symbio.ishenapi.background.model.TmAccessToken;
import com.symbio.ishenapi.background.util.HttpUtils;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.schedule.annotation.Cron;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务，每天晚上12点重新获取一次
 */
@Cron(value="*/59 */23 * * *",daemon = true)
public class monitor extends BaseController implements Runnable{
    @Override
    public void run() {
        System.out.println("执行了拦截器"+new Date());
        Log logger = LogFactory.getLog(TmHospitalInfoController.class);

        String url = "https://open.jss.com.cn/accessToken?";
        String param = "utf-8";
        Map<String,String> map = new HashMap<>();
        map.put("client_id","TDl1DviB");
        map.put("client_secret","295D8D42FC9040BF");
        map.put("grant_type","client_credentials");
        String str = HttpUtils.doPost(url,map,param);
        System.out.println("===="+str);
        String s1 = str.replaceAll("\\\\", "\"").replaceAll("\"", "");//截掉\和""
        System.out.println(s1);
        JSONObject jsonObj1 = new JSONObject(s1);//json字符串转换成json对象
        System.out.println(jsonObj1);
        //JSONObject personObj1 = jsonObj1.getJSONObject("expires_in");
        System.out.println(jsonObj1);
        Object expires_in = jsonObj1.get("expires_in");//获取密码*/
        Object access_token = jsonObj1.get("access_token");//获取密码*/
        System.out.println(expires_in);
        System.out.println(access_token);

        TmAccessToken tmAccessToken = new TmAccessToken();
        tmAccessToken.setAccessToken(access_token.toString());
        tmAccessToken.setExpiresIn(expires_in.toString());
        tmAccessToken.setStatus(1L);
        tmAccessToken.setCreateTime(new Date());
        if(tmAccessToken.save()){
            renderJson(40001);
        }else{
            renderError(40004,"token保存");
        }
    }
}
